import 'package:flutter/material.dart';
import 'package:my_flutter_github/MyStyle.dart';
import 'package:my_flutter_github/common/dao/UserDao.dart';
import 'package:my_flutter_github/model/Notification.dart';
import 'package:my_flutter_github/utils/CommonUtils.dart';
import 'package:my_flutter_github/utils/NavigatorUtils.dart';
import 'package:my_flutter_github/widgets/BaseListState.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:my_flutter_github/widgets/PullLoadWidget.dart';
import 'package:my_flutter_github/widgets/SelectItemWidget.dart';
import 'package:my_flutter_github/widgets/TitleBarWidget.dart';
import 'package:my_flutter_github/widgets/UserEventItemWidget.dart';

/// 通知消息
class NotifyPage extends StatefulWidget {
  NotifyPage();

  @override
  _NotifyPageState createState() => _NotifyPageState();
}

// ignore: mixin_inherits_from_not_object
class _NotifyPageState extends BaseListState<NotifyPage> {
  final SlidableController slidableController = new SlidableController();

  int selectIndex = 0;

  _NotifyPageState();

  _renderItem(index) {
    UserNotification notification = pullLoadWidgetControl.dataList[index];
    if (selectIndex != 0) {
      return _renderEventItem(notification);
    }
    return new Slidable(
      controller: slidableController,
      delegate: new SlidableDrawerDelegate(),
      actionExtentRatio: 0.25,
      child: _renderEventItem(notification),
      secondaryActions: <Widget>[
        new IconSlideAction(
          caption: "已读",
          color: Colors.redAccent,
          icon: Icons.delete,
          onTap: () {
            UserDao.setNotificationAsReadDao(notification.id.toString()).then((res) {
              showRefreshLoading();
            });
          },
        ),
      ],
    );
  }

  _renderEventItem(UserNotification notification) {
    EventViewModel eventViewModel = EventViewModel.fromNotify(context, notification);
    return new UserEventItemWidget(eventViewModel, onPressed: () {
      if (notification.unread) {
        UserDao.setNotificationAsReadDao(notification.id.toString());
      }
      if (notification.subject.type == 'Issue') {
        String url = notification.subject.url;
        List<String> tmp = url.split("/");
        String number = tmp[tmp.length - 1];
        String userName = notification.repository.owner.login;
        String reposName = notification.repository.name;
        NavigatorUtils.goIssueDetail(context, userName, reposName, number, needRightLocalIcon: true).then((res) {
          showRefreshLoading();
        });
      }
    }, needImage: false);
  }

  _resolveSelectIndex() {
    clearData();
    showRefreshLoading();
  }

  _getDataLogic() async {
    return await UserDao.getNotifyDao(selectIndex == 2, selectIndex == 1, pageIndex);
  }

  @override
  bool get wantKeepAlive => true;

  @override
  bool get needHeader => false;

  @override
  bool get autoRefresh => true;

  @override
  requestLoadMore() async {
    return await _getDataLogic();
  }

  @override
  requestRefresh() async {
    return await _getDataLogic();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context); // See AutomaticKeepAliveClientMixin.
    return new Scaffold(
      backgroundColor: Color(MyColors.mainBackgroundColor),
      appBar: new AppBar(
        title: TitleBarWidget(
          title: "通知",
          iconData: MyIcons.NOTIFY_ALL_READ,
          needRightLocalIcon: true,
          onPressed: () {
            CommonUtils.showLoadingDialog(context);
            UserDao.setAllNotificationAsReadDao().then((res) {
              Navigator.pop(context);
              _resolveSelectIndex();
            });
          },
        ),
        bottom: new SelectItemWidget(
          [
            "未读",
            "参与",
            "所有",
          ],
          (selectIndex) {
            this.selectIndex = selectIndex;
            _resolveSelectIndex();
          },
          height: 30.0,
          margin: const EdgeInsets.all(0.0),
          elevation: 0.0,
        ),
        elevation: 4.0,
      ),
      body: PullLoadWidget(
        control: pullLoadWidgetControl,
        itemBuilder: (BuildContext context, int index) => _renderItem(index),
        refreshCallback: onRefresh,
        loadMoreCallback: onLoadMore,
        refreshKey: refreshIndicatorKey,
      ),
    );
  }
}
